package as;

import java.io.IOException;
import java.sql.*;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.*;
/**
 * Servlet implementation class UserServ
 */
@WebServlet("/UserServ")
public class UserServ extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServ() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		String id = request.getParameter("id");
		String pw = request.getParameter("pw");

	       String mailaddress = request.getParameter("mailaddress");
	       String kokyakumeikanji = request.getParameter("kokyakumeikanji");
	        String kokyakumeikana = request.getParameter("kokyakumeikana");
	        String nyuusya = request.getParameter("nyuusya");
	       String yakusyoku = request.getParameter("yakusyoku");

		Connection conn = null;

		String url = "jdbc:mysql://localhost:3306/mysql";
		String user = "root";
		String password = "85428133";



		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			conn = DriverManager.getConnection(url, user, password);
			Statement stmt = conn.createStatement();
			//String sql = "INSERT INTO UserInfo (Password,emailaddress,NameKanji,NameKana,HiredYYYYMM,Position) VALUES(pw,mailaddress,kokyakumeikanji,kokyakumeikana,nyuusya,yakusyoku)";
			String sql = "INSERT INTO userinfo"+"("+"ID,Password,emailaddress,NameKanji,NameKana,HiredYYYYMM,Position"+")"+" VALUES('"+id+"','"+pw+"','"+mailaddress+"','"+kokyakumeikanji+"','"+kokyakumeikana+"','"+nyuusya+"','"+yakusyoku+"'"+")";

			//String sql = "INSERT INTO UserInfo(Password,emailaddress,NameKanji,NameKana,HiredYYYYMM,Position) VALUES(pw,mailaddress,kokyakumeikanji,kokyakumeikana,nyuusya,yakusyoku)";

			//		int rs = stmt.executeUpdate(sql);
			//stmt.executeUpdate(sql);
			stmt.executeUpdate(sql);
					stmt.close();
//					pstmt.close();
		 		conn.close();
		} catch (SQLException e) {
			// TODO 自動生成された catch ブロック
			System.out.println("Sql exception");
		} catch (ClassNotFoundException e) {
			System.out.println("Class not found");

		}
		catch (Exception e) {
			System.out.println("exception");
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				response.getWriter().println(e.getMessage());
			}
		}
		response.sendRedirect("jsp/loginsuccess.jsp");
	}

}
